﻿using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.DataAccess.DO
{
    public partial class PurchaseOrderHeaderDO : BaseDO
    {
        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static PurchaseOrderHeaderDO GetModelByPK(Session sess, int iIntPONum)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel + " WHERE INTERNAL_PURCHASE_ORDER_NUM = @INTERNAL_PURCHASE_ORDER_NUM";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_PURCHASE_ORDER_NUM", iIntPONum)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性INTERNAL_PURCHASE_ORDER_NUM也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public int Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "RCP_IPOHeader";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "DELETE FROM PURCHASE_ORDER_HEADER WHERE INTERNAL_PURCHASE_ORDER_NUM = @INTERNAL_PURCHASE_ORDER_NUM";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.Text, stSQL, UpdateWherePara(this));
                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public void Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "RCP_UPOHeader";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string Update(Session sess, Dictionary<string, object> dcData)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            string stSQL = null;
            int i = 0;
            IDataParameter[] paras = null;

            try
            {
                if (dcData.Count < 1)
                    return null;
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(256)
                            .Append(" UPDATE PURCHASE_ORDER_HEADER SET ");
                paras = new IDataParameter[dcData.Count + 1];
                foreach (KeyValuePair<string, object> kvp in dcData)
                {
                    sbSQL.Append(kvp.Key).Append(" = @Value").Append(i).Append(",");
                    paras[i] = new SqlParameter("@Value" + i, kvp.Value);
                    i++;
                }

                stSQL = sbSQL.ToString();
                stSQL = stSQL.Substring(0, stSQL.Length - 1);

                stSQL = stSQL + " WHERE INTERNAL_PURCHASE_ORDER_NUM = @INTERNAL_PURCHASE_ORDER_NUM";
                paras[i] = new SqlParameter("@INTERNAL_PURCHASE_ORDER_NUM", this.INTERNAL_PURCHASE_ORDER_NUM);

                helper.Update(CommandType.Text, stSQL, paras);

                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象RECEIPHEADER的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<PurchaseOrderHeaderDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "SP_RECEIPHEADER_SelectAll";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性RECEIPHEADER对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<PurchaseOrderHeaderDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = PurchaseOrderHeaderDO.SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    //DataHelper.BuildParameter(sess, "@Value", value)
                    new SqlParameter( "@Value", value)
                };
                dtModel = helper.GetTable(CommandType.Text, stSQL, paras);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(de.Value.Key).Append(" @Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value);
                    i++;
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        /// <summary>
        /// 从DataView装载持久性RECEIPHEADER对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<PurchaseOrderHeaderDO> LoadListFromDataView(DataView dv)
        {
            List<PurchaseOrderHeaderDO> list = new List<PurchaseOrderHeaderDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static PurchaseOrderHeaderDO GetModel(DataRow row)
        {
            PurchaseOrderHeaderDO poHdrDO = new PurchaseOrderHeaderDO();
            if (!row["INTERNAL_PURCHASE_ORDER_NUM"].Equals(DBNull.Value)) poHdrDO.INTERNAL_PURCHASE_ORDER_NUM = Convert.ToInt32(row["INTERNAL_PURCHASE_ORDER_NUM"]);
            if (!row["WAREHOUSE"].Equals(DBNull.Value)) poHdrDO.WAREHOUSE = Convert.ToString(row["WAREHOUSE"]);
            if (!row["COMPANY"].Equals(DBNull.Value)) poHdrDO.COMPANY = Convert.ToString(row["COMPANY"]);
            if (!row["PURCHASE_ORDER_ID"].Equals(DBNull.Value)) poHdrDO.PURCHASE_ORDER_ID = Convert.ToString(row["PURCHASE_ORDER_ID"]);
            if (!row["PURCHASE_ORDER_TYPE"].Equals(DBNull.Value)) poHdrDO.PURCHASE_ORDER_TYPE = Convert.ToString(row["PURCHASE_ORDER_TYPE"]);
            if (!row["CLOSE_DATE"].Equals(DBNull.Value)) poHdrDO.CLOSE_DATE = Convert.ToDateTime(row["CLOSE_DATE"]);
            if (!row["ERP_ORDER_ID"].Equals(DBNull.Value)) poHdrDO.ERP_ORDER_ID = Convert.ToString(row["ERP_ORDER_ID"]);
            if (!row["STATUS"].Equals(DBNull.Value)) poHdrDO.STATUS = Convert.ToInt32(row["STATUS"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) poHdrDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) poHdrDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["SHIP_FROM"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM = Convert.ToString(row["SHIP_FROM"]);
            if (!row["SHIP_FROM_ADDRESS1"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_ADDRESS1 = Convert.ToString(row["SHIP_FROM_ADDRESS1"]);
            if (!row["SHIP_FROM_ADDRESS2"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_ADDRESS2 = Convert.ToString(row["SHIP_FROM_ADDRESS2"]);
            if (!row["SHIP_FROM_CITY"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_CITY = Convert.ToString(row["SHIP_FROM_CITY"]);
            if (!row["SHIP_FROM_STATE"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_STATE = Convert.ToString(row["SHIP_FROM_STATE"]);
            if (!row["SHIP_FROM_COUNTRY"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_COUNTRY = Convert.ToString(row["SHIP_FROM_COUNTRY"]);
            if (!row["SHIP_FROM_POSTAL_CODE"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_POSTAL_CODE = Convert.ToString(row["SHIP_FROM_POSTAL_CODE"]);
            if (!row["SHIP_FROM_NAME"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_NAME = Convert.ToString(row["SHIP_FROM_NAME"]);
            if (!row["SHIP_FROM_ATTENTION_TO"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_ATTENTION_TO = Convert.ToString(row["SHIP_FROM_ATTENTION_TO"]);
            if (!row["SHIP_FROM_EMAIL_ADDRESS"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_EMAIL_ADDRESS = Convert.ToString(row["SHIP_FROM_EMAIL_ADDRESS"]);
            if (!row["SHIP_FROM_PHONE_NUM"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_PHONE_NUM = Convert.ToString(row["SHIP_FROM_PHONE_NUM"]);
            if (!row["SHIP_FROM_FAX_NUM"].Equals(DBNull.Value)) poHdrDO.SHIP_FROM_FAX_NUM = Convert.ToString(row["SHIP_FROM_FAX_NUM"]);
            if (!row["SCHEDULED_ARRIVE_DATE"].Equals(DBNull.Value)) poHdrDO.SCHEDULED_ARRIVE_DATE = Convert.ToDateTime(row["SCHEDULED_ARRIVE_DATE"]);
            if (!row["ACTUAL_ARRIVE_DATE"].Equals(DBNull.Value)) poHdrDO.ACTUAL_ARRIVE_DATE = Convert.ToDateTime(row["ACTUAL_ARRIVE_DATE"]);

            return poHdrDO;
        }
        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        public static string SelectSequel
        {
            get
            {
                return "Select	*  From [PURCHASE_ORDER_HEADER] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDataParameter[] ValueParas(PurchaseOrderHeaderDO poHdrDO)
        {
            SqlParameter[] paras = new SqlParameter[25];
            paras[0] = new SqlParameter("@INTERNAL_PURCHASE_ORDER_NUM", poHdrDO.INTERNAL_PURCHASE_ORDER_NUM);
            paras[1] = new SqlParameter("@WAREHOUSE", poHdrDO.WAREHOUSE == String.Empty ? DBNull.Value : (object)poHdrDO.WAREHOUSE);
            paras[2] = new SqlParameter("@COMPANY", poHdrDO.COMPANY == String.Empty ? DBNull.Value : (object)poHdrDO.COMPANY);
            paras[3] = new SqlParameter("@PURCHASE_ORDER_ID", poHdrDO.PURCHASE_ORDER_ID == String.Empty ? DBNull.Value : (object)poHdrDO.PURCHASE_ORDER_ID);
            paras[4] = new SqlParameter("@PURCHASE_ORDER_TYPE", poHdrDO.PURCHASE_ORDER_TYPE == String.Empty ? DBNull.Value : (object)poHdrDO.PURCHASE_ORDER_TYPE);
            paras[5] = new SqlParameter("@CLOSE_DATE", poHdrDO.CLOSE_DATE == DateTime.MinValue ? DBNull.Value : (object)poHdrDO.CLOSE_DATE);
            paras[6] = new SqlParameter("@PRIORITY", poHdrDO.PRIORITY);
            paras[7] = new SqlParameter("@ERP_ORDER_ID", poHdrDO.ERP_ORDER_ID == String.Empty ? DBNull.Value : (object)poHdrDO.ERP_ORDER_ID);
            paras[8] = new SqlParameter("@STATUS", poHdrDO.STATUS);
            paras[9] = new SqlParameter("@USER_STAMP", poHdrDO.USER_STAMP == String.Empty ? DBNull.Value : (object)poHdrDO.USER_STAMP);
            paras[10] = new SqlParameter("@DATE_TIME_STAMP", poHdrDO.DATE_TIME_STAMP == DateTime.MinValue ? DBNull.Value : (object)poHdrDO.DATE_TIME_STAMP);
            paras[11] = new SqlParameter("@SHIP_FROM", poHdrDO.SHIP_FROM == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM);
            paras[12] = new SqlParameter("@SHIP_FROM_ADDRESS1", poHdrDO.SHIP_FROM_ADDRESS1 == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_ADDRESS1);
            paras[13] = new SqlParameter("@SHIP_FROM_ADDRESS2", poHdrDO.SHIP_FROM_ADDRESS2 == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_ADDRESS2);
            paras[14] = new SqlParameter("@SHIP_FROM_CITY", poHdrDO.SHIP_FROM_CITY == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_CITY);
            paras[15] = new SqlParameter("@SHIP_FROM_STATE", poHdrDO.SHIP_FROM_STATE == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_STATE);
            paras[16] = new SqlParameter("@SHIP_FROM_COUNTRY", poHdrDO.SHIP_FROM_COUNTRY == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_COUNTRY);
            paras[17] = new SqlParameter("@SHIP_FROM_POSTAL_CODE", poHdrDO.SHIP_FROM_POSTAL_CODE == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_POSTAL_CODE);
            paras[18] = new SqlParameter("@SHIP_FROM_NAME", poHdrDO.SHIP_FROM_NAME == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_NAME);
            paras[19] = new SqlParameter("@SHIP_FROM_ATTENTION_TO", poHdrDO.SHIP_FROM_ATTENTION_TO == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_ATTENTION_TO);
            paras[20] = new SqlParameter("@SHIP_FROM_EMAIL_ADDRESS", poHdrDO.SHIP_FROM_EMAIL_ADDRESS == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_EMAIL_ADDRESS);
            paras[21] = new SqlParameter("@SHIP_FROM_PHONE_NUM", poHdrDO.SHIP_FROM_PHONE_NUM == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_PHONE_NUM);
            paras[22] = new SqlParameter("@SHIP_FROM_FAX_NUM", poHdrDO.SHIP_FROM_FAX_NUM == String.Empty ? DBNull.Value : (object)poHdrDO.SHIP_FROM_FAX_NUM);
            paras[23] = new SqlParameter("@SCHEDULED_ARRIVE_DATE", poHdrDO.SCHEDULED_ARRIVE_DATE == DateTime.MinValue ? DBNull.Value : (object)poHdrDO.SCHEDULED_ARRIVE_DATE);
            paras[24] = new SqlParameter("@ACTUAL_ARRIVE_DATE", poHdrDO.ACTUAL_ARRIVE_DATE == DateTime.MinValue ? DBNull.Value : (object)poHdrDO.ACTUAL_ARRIVE_DATE);

            paras[0].DbType = DbType.Int32;
            paras[1].DbType = DbType.String;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.String;
            paras[4].DbType = DbType.String;
            paras[5].DbType = DbType.Date;
            paras[6].DbType = DbType.Int32;
            paras[7].DbType = DbType.String;
            paras[8].DbType = DbType.Decimal;
            paras[9].DbType = DbType.String;
            paras[10].DbType = DbType.DateTime;
            paras[11].DbType = DbType.String;
            paras[12].DbType = DbType.String;
            paras[13].DbType = DbType.String;
            paras[14].DbType = DbType.String;
            paras[15].DbType = DbType.String;
            paras[16].DbType = DbType.String;
            paras[17].DbType = DbType.String;
            paras[18].DbType = DbType.String;
            paras[19].DbType = DbType.String;
            paras[20].DbType = DbType.String;
            paras[21].DbType = DbType.String;
            paras[22].DbType = DbType.String;
            paras[23].DbType = DbType.Date;
            paras[24].DbType = DbType.Date;


            return paras;
        }

        private static IDataParameter[] UpdateWherePara(PurchaseOrderHeaderDO poHdrDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_PURCHASE_ORDER_NUM", poHdrDO.INTERNAL_PURCHASE_ORDER_NUM);
            return paras;
        }

        #endregion

    }
}
